<template>
  <ele-modal
    :width="950"
    :visible="visible"
    :confirm-loading="loading"
    :title="'新建'"
    :body-style="{ paddingBottom: '8px' }"
    @update:visible="updateVisible"
    @ok="save"
  >
    <a-form
      :label-col="{ md: { span: 7 }, sm: { span: 4 }, xs: { span: 24 } }"
      :wrapper-col="{ md: { span: 17 }, sm: { span: 20 }, xs: { span: 24 } }"
    >
      <a-row :gutter="24">
        <a-col :md="24" :sm="24" :xs="24">
          <a-form-item label="单据类型" v-bind="validateInfos.type">
            <a-radio-group v-model:value="form.type" @change="change">
              <a-radio :value="1">收入</a-radio>
              <a-radio :value="2">支出</a-radio>
            </a-radio-group>
          </a-form-item>
          <a-form-item label="费用类型" v-bind="validateInfos.fee_name_id">
            <a-select
              optionFilterProp="label"
              :options="feeOptions"
              allow-clear
              v-model:value="form.fee_name_id"
              placeholder="请选择"
              autocomplete="off"
            />
          </a-form-item>
          <a-form-item label="金额" v-bind="validateInfos.amount">
            <a-input
              allow-clear
              placeholder="请输入"
              v-model:value="form.amount"
              autocomplete="off"
            />
          </a-form-item>
          <a-form-item label="支付方式" v-bind="validateInfos.pay_type">
            <a-select
              optionFilterProp="label"
              :options="payOptions"
              allow-clear
              v-model:value="form.pay_type"
              placeholder="请选择"
              autocomplete="off"
            />
          </a-form-item>
          <a-form-item label="业务日期" v-bind="validateInfos.business_time">
            <a-date-picker
              class="ele-fluid"
              valueFormat="YYYY/MM/DD"
              placeholder="请选择业务日期"
              v-model:value="form.business_time"
              autocomplete="off"
            />
          </a-form-item>
        </a-col>
      </a-row>
    </a-form>
  </ele-modal>
</template>

<script setup>
  import { ref, reactive, onMounted } from 'vue';
  import { Form } from 'ant-design-vue';
  import { uploadUrl } from '@/config/setting';
  import { emailReg, phoneReg, identityReg } from 'ele-admin-pro';
  import { PlusOutlined, LoadingOutlined } from '@ant-design/icons-vue';
  import selectMember from '../../../order/jewelry_order/components/select-member.vue';
  import { get_fee_name_select } from '@/api/material/select';
  import { get_pay_type_select } from '@/api/setting/pay_type';
  import { get_repair_order_detail } from '@/api/order/other_balance';

  // 表格列配置
  const useForm = Form.useForm;

  const props = defineProps({
    // 弹窗是否打开
    visible: Boolean,
    order_sn: String
  });

  const emit = defineEmits(['done', 'update:visible']);
  // 提交状态
  const loading = ref(false);
  // 表单数据
  const form = reactive({
    order_sn: props.order_sn,
    type: 1,
    fee_name_id: null,
    amount: '',
    pay_type: null,
    business_time: ''
  });

  const feeOptions = ref([]);
  const payOptions = ref([]);
  onMounted(async () => {
    if (props.order_sn) {
      await get_repair_order_detail({ order_sn: props.order_sn }).then(
        (res) => {
          Object.keys(form).forEach((key) => {
            form[key] = res[key] ? res[key] : null;
          });
          console.log('form', form);
        }
      );
    }
    await getOptions();
    await get_pay_type_select({ page: 1, page_size: 99999 }).then((res) => {
      payOptions.value = res;
    });
  });

  const change = (e) => {
    form.fee_name_id = '';
    getOptions();
  };
  const getOptions = () => {
    get_fee_name_select({
      filters: { type: form.type },
      page: 1,
      page_size: 99999
    }).then((res) => {
      feeOptions.value = res;
    });
  };
  // 表单验证规则
  const rules = reactive({
    type: [
      {
        required: true,
        message: '请选择单据类型',
        type: 'number',
        trigger: 'change'
      }
    ],
    fee_name_id: [
      {
        required: true,
        message: '请选择费用类型',
        type: 'number',
        trigger: 'change'
      }
    ],
    amount: [
      {
        required: true,
        message: '请输入金额',
        type: 'string',
        trigger: 'change'
      }
    ],
    pay_type: [
      {
        required: true,
        message: '请选择支付方式',
        type: 'string',
        trigger: 'change'
      }
    ],
    business_time: [
      {
        required: true,
        message: '请选择业务日期',
        type: 'string',
        trigger: 'change'
      }
    ]
  });

  const { validate, validateInfos } = useForm(form, rules);
  /* 保存编辑 */
  const save = () => {
    validate()
      .then(() => {
        emit('done', form);
      })
      .catch(() => {});
  };
  /* 更新visible */
  const updateVisible = (value) => {
    emit('update:visible', value);
  };
</script>
<style scoped></style>
